//logs.js
const util = require('../../utils/util.js')
import request from "../../utils/request";
var api = require("../../utils/api");
const app = getApp()
const {
  $Message
} = require('../commpoents/iview-dist/base/index.js');
var innerAudioContext = null
Page({
  data: {
    isadmin: '',
    resultContent: '',
    leader_time: '',
    check_time: '',
    money: 0,
    listType: '',
    level: 1, //级别
    check_status: 0,
    days: 3, // 默认天数
    showSloveDay: false, // 是否显示处理天数
    sloveDays: 0, // 处理级别时限
    id: 0,
    getMoney: 0, //追加奖励
    award: 1, // 追加奖励1，不追加2
    textNowL: 0,
    content: "",
    textNowL2: 0,
    content2: "",
    textNowL3: 0,
    content3: "",
    textNowL4: 0,
    content4: "",
    textNowL5: 0,
    content5: "",
    mainPerson: "", //主责人
    classId: 0,
    classId1: 2,
    classId2: 4,
    details: {},
    visible1: false,
    sumTxtL: 100000,
    actions1: [{
        name: '虚假信息',
      },
      {
        name: '无效投诉'
      },
      {
        name: '其他'
      },
      // {
      //   name: '去分享',
      //   icon: 'share',
      //   openType: 'share'
      // }
    ],
    cause: "虚假信息",
    isOther: false,
    isBoss: false,
    onActiveId: null,
  },
  onUnload() {
    innerAudioContext.destroy()
  },
  onLoad(option) {
    if (app.checkOpenid()){
      this.setData({
        isadmin: app.globalData.loginInfo.isadmin
      })
    }
    var postId = option.rowId;
    if (postId) {
      this.setData({
        id: postId,
        listType: option.listType
      });
    }
  },
  onShow(){
    innerAudioContext = wx.createInnerAudioContext()
    this.setData({
      isBoss: app.globalData.isBoss
    })
    this.fetchData(this.data.id);
  },
  // 播放语音
  startVoice(e) {
    var self = this
    var a1 = 0
    var a2 = 0
    var a3 = 0
    var a4 = 0
    var playItem = e.currentTarget.dataset.item
    debugger
    var lastPlayItem = ''
    innerAudioContext.src = playItem
    innerAudioContext.obeyMuteSwitch = false
    innerAudioContext.play()
    self.setData({
      onActiveId: e.currentTarget.dataset.id
    })
    innerAudioContext.onPlay(() => {
      console.log('开始播放')
     
    })
    innerAudioContext.onEnded(() => {
      console.log('播放结束')
      self.setData({
        onActiveId: null
      })
      innerAudioContext.stop()
    })
    // innerAudioContext.onStop(() => {
    //   console.log('播放tz')
    //   self.setData({
    //     onActiveId: null
    //   })
    //   innerAudioContext.stop()
    // })
    innerAudioContext.onError((res) => {
      self.setData({
        onActiveId: null
      })
      innerAudioContext.stop()
    })
  },
  onBossBtn() {
    debugger
    innerAudioContext.destroy()
    wx.navigateTo({
      url: '/pages/boss/boss?rowId=' + this.data.id,
    })
  },
  onFile(e) {
    innerAudioContext.destroy()
    var fileUrl = e.currentTarget.dataset.fileurl
    //检查是否为文档doc, xls, ppt, pdf, docx, xlsx, pptx
    var isDoc = (/.doc$|.xls$|.ppt$|.docx$|.xlsx$|.pptx$|.pdf$/i)
    if (isDoc.test(fileUrl)) {
      wx.downloadFile({
        url: fileUrl,
        success: function (res) {
          var filePath = res.tempFilePath
          wx.openDocument({
            filePath: filePath,
            success: function (res) {
              console.log('打开文档成功' + filePath)
            }
          })
        }
      })
    } else {
      wx.navigateTo({
        url: '/pages/details/details/webview/webview?fileUrl=' + fileUrl,
      })
    }
  },
  handleOpen1() {
    this.setData({
      visible1: true
    });
  },
  handleCancel1() {
    this.setData({
      visible1: false
    });
  },
  changeInput(e) {
    var num = e.detail.cursor
    if (num >= this.data.sumTxtL) {
      $Message({
        content: '最多可写' + this.data.sumTxtL + '字',
        type: 'warning'
      });
      return
    }
    this.setData({
      textNowL: num,
      content: e.detail.value
    })
  },
  changeInput2(e) {
    var num = e.detail.cursor
    if (num >= this.data.sumTxtL) {
      $Message({
        content: '最多可写' + this.data.sumTxtL + '字',
        type: 'warning'
      });
      return
    }
    this.setData({
      textNowL2: num,
      content2: e.detail.value
    })
  },
  changeInput3(e) {
    var num = e.detail.cursor
    if (num >= this.data.sumTxtL) {
      $Message({
        content: '最多可写' + this.data.sumTxtL + '字',
        type: 'warning'
      });
      return
    }
    this.setData({
      textNowL3: num,
      content3: e.detail.value
    })
  },
  changeInput4(e) {
    var num = e.detail.cursor
    if (num >= this.data.sumTxtL) {
      $Message({
        content: '最多可写' + this.data.sumTxtL + '字',
        type: 'warning'
      });
      return
    }
    this.setData({
      textNowL4: num,
      content4: e.detail.value
    })
  },
  changeInput5(e) {
    var num = e.detail.cursor
    if (num >= this.data.sumTxtL) {
      $Message({
        content: '最多可写' + this.data.sumTxtL + '字',
        type: 'warning'
      });
      return
    }
    this.setData({
      textNowL5: num,
      content5: e.detail.value
    })
  },
  handleClickItem1({
    detail
  }) {
    const index = Number(detail.index);
    this.setData({
      cause: this.data.actions1[index].name,
      visible1: false
    });
    if (index === 2) {
      this.setData({
        isOther: true
      });
    } else {
      this.setData({
        isOther: false
      });
    }
  },
  //选择处理时限
  onSolveDays(e) {
    this.setData({
      days: e.detail.value
    })
  },
  getDays(e) {
    if (e === 1) {
      return 3
    } else if (e === 2) {
      return 7
    } else {
      return 0 // 为自定义
    }
  },
  onLevel(e) {
    this.setData({
      level: Number(e.currentTarget.dataset.level),
      days: this.getDays(Number(e.currentTarget.dataset.level))
    })
    if (e.currentTarget.dataset.level === 3) {
      //如果是其他，则选择处理时限
      this.setData({
        showSloveDay: true,
      })
    }
  },
  onTab1(e) {
    this.setData({
      classId1: Number(e.currentTarget.dataset.id)
    })
  },
  onTab2(e) {
    this.setData({
      classId2: Number(e.currentTarget.dataset.id)
    })
  },
  onTab3(e) {
    this.setData({
      award: Number(e.currentTarget.dataset.id)
    })
  },
  onTab(e) {
    this.setData({
      classId: Number(e.currentTarget.dataset.id)
    })
  },
  onInput(e) {
    var money = Number(e.detail.value)
    this.setData({
      getMoney: money
    })
  },
  fetchData(id) {
    var url = api.SecretDetails
    var params = {
      id: id,
      isadmin: app.globalData.loginInfo.isadmin
    }
    request(url, params, "POST").then(res => {
      if (res.code == 0) {
        var result = res.data;
        var files = this.resolveFileStr(result)
        var leader_time = util.formatTime(new Date(result.leader_time * 1000))
        var check_time = util.formatTime(new Date(result.check_time * 1000))
        var resultContent = util.escape2Html(result.content)
        debugger
        this.setData({
          resultContent: resultContent,
          leader_time: leader_time,
          check_time: check_time,
          details: result,
          fileArr: files,
          money: Number(result.append_bonus) + Number(result.bonus)
        })
      }
    })
  },
  resolveFileStr(e) {
    var fileArr = e.file.split(',')
    var allFile = []
    fileArr.forEach(item => {
      var obj = {
        name: '',
        url: '',
      }
      var lastStr = item.split('/')
      obj.name = lastStr[lastStr.length - 1]
      obj.url = item
      allFile.push(obj)
    })
    return allFile
  },
  subFun1() {
    var self = this
    if (this.data.classId1 === 2) {
      var params = {
        id: this.data.id,
        check_status: this.data.classId1
      }
    } else {
      if (this.data.isOther) {
        if (this.data.content !== '' && this.data.content) {
          var params = {
            id: this.data.id,
            check_status: this.data.classId1,
            check_reply: this.data.cause + "--" + this.data.content
          }
        } else {
          $Message({
            content: '原因不能为空',
            type: 'warning'
          });
          return
        }
      } else {
        var params = {
          id: this.data.id,
          check_status: this.data.classId1,
          check_reply: this.data.cause
        }
      }
    }
    var url = api.SecretCheck
    request(url, params, "POST").then(res => {
      if (res.code == 0) {
        wx.redirectTo({
          url: '/pages/checkResultList/checkResultList?listType=' + this.data.listType,
        })
        wx.navigateBack({
          delta: 1
        })
      } else if (res.code === "1020011") {
        self.showErr(res)
      }
    })
  },
  showErr(res) {
    wx.showModal({
      content: '需要关注公众号才能收到消息哦',
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          wx.previewImage({
            current: 'https://neikong.chinahonden.com/static/module/api/imgs/ewm.jpg', // 当前显示图片的http链接
            urls: ['https://neikong.chinahonden.com/static/module/api/imgs/ewm.jpg'] // 需要预览的图片http链接列表
          })
          wx.navigateBack({
            delta: 1,
          })
        } else if (res.cancel) {

        }
      }
    })
  },
  changePerson(e) {
    this.setData({
      mainPerson: e.detail.value
    })
  },
  subFun2() {
    var self = this
    if (this.data.classId2 === 4) {
      if (this.data.mainPerson !== '' && this.data.mainPerson && this.data.content2 !== '' && this.data.content2) {
        var params = {
          id: this.data.id,
          check_status: this.data.classId2,
          leader: this.data.mainPerson,
          arrange: this.data.content2
        }
      } else {
        $Message({
          content: '主责人和督查安排不能为空',
          type: 'warning'
        });
        return
      }
    } else {
      if (this.data.isOther) {
        if (this.data.content3 !== '' && this.data.content3) {
          var params = {
            id: this.data.id,
            check_status: this.data.classId2,
            check_reply: this.data.cause + "--" + this.data.content3
          }
        } else {
          $Message({
            content: '原因不能为空',
            type: 'warning'
          });
          return
        }
      } else {
        var params = {
          id: this.data.id,
          check_status: this.data.classId2,
          check_reply: this.data.cause,
        }
      }
    }
    if (this.data.details.check_status === 2) {
      params.days = this.data.days // 处理天数

      if (this.data.days <= 0) {
        $Message({
          content: '处理天数不能为0',
          type: 'warning'
        });
        return
      }
    }
    var url = api.SecretAccept
    request(url, params, "POST").then(res => {
      if (res.code == 0) {
        wx.redirectTo({
          url: '/pages/checkResultList/checkResultList?listType=' + this.data.listType,
        })
        wx.navigateBack({
          delta: 1
        })
      } else if (res.code === "1020011") {
        self.showErr(res)
      }
    })
  },
  subFun3() {
    var self = this
    if (this.data.content4 !== '' && this.data.content4 && this.data.content5 !== '' && this.data.content5) {
      if (this.data.award === 1) {
        var params = {
          id: this.data.id,
          survey_results: this.data.content4,
          process_results: this.data.content5
        }
      } else {
        var params = {
          id: this.data.id,
          survey_results: this.data.content4,
          process_results: this.data.content5,
          append_bonus: this.data.getMoney
        }
      }
    } else {
      $Message({
        content: '结果不能为空',
        type: 'warning'
      });
      return
    }
    var url = api.SecretProcess
    request(url, params, "POST").then(res => {
      if (res.code == 0) {
        wx.redirectTo({
          url: '/pages/checkResultList/checkResultList?listType=' + this.data.listType,
        })
        wx.navigateBack({
          delta: 1
        })
      } else if (res.code === "1020011") {
        self.showErr(res)
      }
    })
  }
})